/*
* This is a simple server application
* This server receive a string message from the Android mobile phone
* and show it on the console.
* Author by Lak J Comspace
*/
package com.service.simpleserver;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleServer extends Thread {
    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static InputStreamReader inputStreamReader;
    private static BufferedReader bufferedReader;
    private static String message;
    private static PrintWriter mOut;

    public static void main(String[] args) {

        try {
            serverSocket = new ServerSocket(4486);
        } catch (IOException e) {
            System.out.println("Could not listen on port: 4486");
        }

        System.out.println("Server started. Listening to the port 4486");

        while (true) {
            try {


                clientSocket = serverSocket.accept();   //accept the client connection
                inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader); //get the client message
                message = bufferedReader.readLine();

                DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
                Date date = new Date();
                System.out.println(dateFormat.format(date) + " : " + message);

                //sends the message to the client
                mOut = new PrintWriter(clientSocket.getOutputStream(), true);
                mOut.write("I am got you!");
                mOut.flush();
                mOut.close();

                inputStreamReader.close();
                clientSocket.close();

            } catch (IOException ex) {
                System.out.println("Problem in message reading");
            }
        }

    }

}

